/*
 * @(#)AbstractDtsConfigInterface.java   1.0  2021年11月15日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.loadconfig;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.swift.core.env.AbstractConfigInterface;
import com.swift.core.env.ConfigInterface.ConfigKV;
import com.swift.exception.extend.SystemException;
import com.swift.util.exec.PathUtil;
import com.swift.util.type.ByteUtil;
import com.swift.util.type.JsonUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月15日
 */
public abstract class AbstractDtsConfigInterface<T extends ConfigKV> extends AbstractConfigInterface<T> {
    
    public abstract String getFilePath();
    
    protected byte[] loadFile() {
        try {
            InputStream in = new FileInputStream(PathUtil.findOnClassPath(getFilePath()));
            byte[] jsonByte = ByteUtil.inputToByte(in);
            try {
                in.close();
            } catch (IOException e) {
            }
            return jsonByte;
        } catch (Exception e) {
            throw new SystemException("读"+getFilePath()+"文件失败",e);
        }
    }
    
    public void saveConfig(List<T> listT) {
        if(listT==null) return;
        try {
            String json = JsonUtil.toJson(listT);
            BufferedWriter out = new BufferedWriter(new FileWriter(PathUtil.findOnClassPath(getFilePath())));
            out.write(json);
            out.close();
        } catch (IOException e) {
            throw new SystemException("写"+getFilePath()+"文件失败",e);
        }
    }
}
